package com.jftone.bh.app.data;

import java.util.ArrayList;
import java.util.List;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.service.BaseService;
import org.jftone.component.BeanContext;
import org.jftone.data.CacheException;
import org.jftone.data.memcache.MemcacheTemplate;
import org.jftone.data.redis.RedisTemplate;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;


public class AreaInfoQry {
	private static final String PARA_AREA = "BH_PARA_AREA";
	/**
	 * 创建地区缓存
	 * @throws Exception 
	 */
	public static void createCache() throws Exception{
		String superCode = SysConst.ROOT_CODE;
		try {
			RedisTemplate cache = BeanContext.getBean(RedisTemplate.class);
			List<IData<String, Object>> list = queryArea(superCode);
			cache.put(PARA_AREA+"_"+superCode, list);
			for(IData<String, Object> data : list){
				List<IData<String, Object>> clist = queryArea(data.getString("CODE"));
				cache.put(PARA_AREA+"_"+data.getString("CODE"), clist);
				for(IData<String, Object> cdata : clist){
					cache.put(PARA_AREA+"_"+cdata.getString("CODE"), queryArea(cdata.getString("CODE")));
				}
			}
		} catch (ComponentException e) {
			throw new Exception("加载地区数据错误"+e.getMessage(), e);
		}
	}
	
	private static List<IData<String, Object>> queryArea(String superCode) throws Exception{
		List<IData<String, Object>> list  = null;
		try {
			BaseService service = BeanContext.getBean(BaseService.class);
			list  = service.queryAreaBySuperCode(superCode);
		} catch (Exception e) {
			throw new Exception("查询地区数据错误"+e.getMessage(), e);
		}
		return list;
	}
	
	/**
	 * 根据父区域编码查询下级区域信息
	 * @param superCode
	 * @return
	 * @throws ActionException
	 * @throws CacheException 
	 */
	@SuppressWarnings("unchecked")
	public static List<IData<String, Object>> query(String superCode) throws Exception{
		List<IData<String, Object>> list  = null;
		String key = PARA_AREA+"_"+superCode;
		try {
			RedisTemplate cache = BeanContext.getBean(RedisTemplate.class);
			list = cache.get(key, ArrayList.class);
			if(null==list){
				list  = queryArea(superCode);
				cache.put(key, list);
			}
			
		} catch (ComponentException e) {
			throw new Exception("查询地区缓存数据错误"+e.getMessage(), e);
		}
		return list;
	}
	
	/**
	 * 根据父类编码查找对应编码的区域名称
	 * @param superCode
	 * @param areaCode
	 * @return
	 * @throws Exception
	 */
	public static String getAreaName(String superCode, String areaCode) throws Exception{
		String areaName  = "";
		try {
			List<IData<String, Object>> list = query(superCode);
			if(null!=list){
				for(IData<String, Object> tmp : list){
					if(areaCode.equals(tmp.getString("CODE"))){
						areaName  = tmp.getString("NAME");
						break;
					}
				}
			}
		} catch (Exception e) {
			throw new Exception("获取地区"+areaCode+"名称错误"+e.getMessage(), e);
		}
		return areaName;
	}
	
	public static String getAreaName(String provinceNo, String cityNo, String countyNo) throws Exception{
		String address = "";
		if(provinceNo!=null && !StringUtil.isBlank(provinceNo) && !provinceNo.equals("0")){
			address = getAreaName(SysConst.ROOT_CODE,provinceNo);
			if(cityNo!=null && !StringUtil.isBlank(cityNo) && !cityNo.equals("0")){
				address += getAreaName(provinceNo,cityNo);
				if(countyNo!=null && !StringUtil.isBlank(countyNo)  && !countyNo.equals("0")){
					address += getAreaName(cityNo,countyNo);
				}
			}
		}
		return address;
	}
}
